package com.lo.controller;

import com.github.pagehelper.PageInfo;
import com.lo.common.JsonResult;
import com.lo.entity.Admin;
import com.lo.entity.User;
import com.lo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    //SpirngBoot+MyBatis
    //所有审核过的课程
    @RequestMapping("selectAllExaminedCourse")
    public JsonResult selectAllExaminedCourse(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                      @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                      String search){

        PageInfo pageInfo = adminService.selectAllExaminedCourse(pageNum,pageSize,search);
//        System.out.println(pageInfo);
        JsonResult result = new JsonResult(0,"所有课程",pageInfo);
        return result;
    }

    @RequestMapping("login")
    public JsonResult login(String username, String password,HttpSession session ){
        if ((username==null||"".equals(username.trim()))||
                (password==null||"".equals(password.trim()))){
            return new JsonResult(3,"用户密码为空",null);
        }
        JsonResult result = adminService.login(username,password);
        Admin admin = (Admin) result.getData();
        session.setAttribute("admin",admin);
        return result;
    }

    //所有未审核的课程
    @RequestMapping("selectAllNotExamineCourse")
    public JsonResult selectAllNotExamineCourse(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                                String search){
        PageInfo pageInfo = adminService.selectAllNotExamineCourse(pageNum,pageSize,search);
        JsonResult result = new JsonResult(0,"待审核课程",pageInfo);
        return result;
    }

    //所有待删除课程
    @RequestMapping("selectAllDeletingCourse")
    public JsonResult selectAllDeletingCourse(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                              @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                              String search){
        PageInfo pageInfo = adminService.selectAllDeletingCourse(pageNum,pageSize,search);
        JsonResult result = new JsonResult(0,"待删除课程",pageInfo);
        return result;
    }

    @RequestMapping("selectAllCourse")
    public JsonResult selectAllCourse(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                              @RequestParam(value = "pageSize",required = false,defaultValue = "9")int pageSize,
                                              @RequestParam(value = "controllInfo")String controllInfo,
                                              String search){

        PageInfo pageInfo = adminService.selectAllCourse(pageNum,pageSize,search,controllInfo);
//        System.out.println("all is ok");
//        System.out.println(pageInfo);
        JsonResult result = new JsonResult(0,"所有课程",pageInfo);
        return result;
    }

}
